library ForwardObserverCommand initializer init requires LegacySystem

	private function Trig_FO_Commands_Conditions takes nothing returns boolean
		local unit u = getUnit(null,"FOUnit")
		local player p 

		if(u == null) then
			return false
		endif
	  
		set p = GetOwningPlayer(u)
		if ( p == GetTriggerPlayer()) then
			set u = null
			set p = null
			return true
		endif

		set p = null
		set u = null
		return false
	endfunction

	private function ForwardObserverCommand takes nothing returns nothing
		local integer strlen = StringLength(GetEventPlayerChatString())
		local integer integerValue
		local string messege = GetEventPlayerChatString()
		local string value = SubStringBJ(messege, 3, 6)
		local string mode = SubStringBJ(messege, 1, 2)
		local player p = GetTriggerPlayer()
		
		if( strlen > 6) then
			return
		endif
		
		if( mode == "-s") then
			set integerValue = S2I(value)
			if messege == "-s" or messege == "-shells" then
				call DisplayTimedTextToPlayer(p,0.0,0.0,6.0, "Shells remaining: " + I2S(getInteger(null,"FO_total_shells")))
				call DisplayTimedTextToPlayer(p,0.0,0.0,6.0, "Barrage dispersion: " + I2S(getInteger(null,"FO_dp")))
				call DisplayTimedTextToPlayer(p,0.0,0.0,6.0, "Shells per barrage: " + I2S(getInteger(null,"FO_number_of_shells")))
				call DisplayTimedTextToPlayer(p,0.0,0.0,6.0, "Delivery time: " + I2S(getInteger(null,"FO_barrage_delay")))
			elseif integerValue > 0 and integerValue <= 20 then
				call DisplayTimedTextToPlayer(GetLocalPlayer(), 0.0, 0.0, 4.0, "Shells Per Barrage Set To: " + I2S(integerValue) )
				call putInteger(null,"FO_number_of_shells", integerValue)
			endif
		elseif( mode == "-d") then
			set integerValue = S2I(value)
			if(integerValue < 50 or integerValue > 800)then
				call DisplayTimedTextToPlayer(p,0.0,0.0,6.0, "Input a dispersion value between 50 and 800." )
			else
				call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 4.0, "Barrage Dispersion Set To : " + I2S(integerValue) )
				call putInteger(null,"FO_dp", integerValue)
			endif
		endif   
		
		set p = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
	    set t = CreateTrigger(  )
		call TriggerRegisterPlayerChatEvent( t, Player(0), "-s", false )
		call TriggerRegisterPlayerChatEvent( t, Player(0), "-d", false )
		call TriggerRegisterPlayerChatEvent( t, Player(1), "-s", false )
		call TriggerRegisterPlayerChatEvent( t, Player(1), "-d", false )
		call TriggerRegisterPlayerChatEvent( t, Player(2), "-s", false )
		call TriggerRegisterPlayerChatEvent( t, Player(2), "-d", false )
		call TriggerRegisterPlayerChatEvent( t, Player(3), "-s", false )
		call TriggerRegisterPlayerChatEvent( t, Player(3), "-d", false )
		call TriggerRegisterPlayerChatEvent( t, Player(4), "-s", false )
		call TriggerRegisterPlayerChatEvent( t, Player(4), "-d", false )
		call TriggerRegisterPlayerChatEvent( t, Player(5), "-s", false )
		call TriggerRegisterPlayerChatEvent( t, Player(5), "-d", false )
		call TriggerRegisterPlayerChatEvent( t, Player(6), "-s", false )
		call TriggerRegisterPlayerChatEvent( t, Player(6), "-d", false )
		call TriggerRegisterPlayerChatEvent( t, Player(7), "-s", false )
		call TriggerRegisterPlayerChatEvent( t, Player(7), "-d", false )
		call TriggerRegisterPlayerChatEvent( t, Player(8), "-s", false )
		call TriggerRegisterPlayerChatEvent( t, Player(8), "-d", false )
		call TriggerRegisterPlayerChatEvent( t, Player(9), "-s", false )
		call TriggerRegisterPlayerChatEvent( t, Player(9), "-d", false )
		call TriggerRegisterPlayerChatEvent( t, Player(10), "-s", false )
		call TriggerRegisterPlayerChatEvent( t, Player(10), "-d", false )
		call TriggerRegisterPlayerChatEvent( t, Player(11), "-s", false )
		call TriggerRegisterPlayerChatEvent( t, Player(11), "-d", false )
		call TriggerAddCondition( t, Condition( function Trig_FO_Commands_Conditions ) )
		call TriggerAddAction( t, function ForwardObserverCommand )
	endfunction
endlibrary